package serialization;

import java.io.*;

//宠物类
class Dog implements Serializable {
    private static final long serialVersionUID = 5215709583065948679L;
    private String alias;
    private int kill; //攻击力。

    public Dog() {
    }

    public Dog(String alias, int kill) {
        this.alias = alias;
        this.kill = kill;
    }

    public String getAlias() {
        return alias;
    }

    public void setAlias(String alias) {
        this.alias = alias;
    }

    public int getKill() {
        return kill;
    }

    public void setKill(int kill) {
        this.kill = kill;
    }

    @Override
    public String toString() {
        return "Dog{" +
                "alias='" + alias + '\'' +
                ", kill=" + kill +
                '}';
    }
}

//游戏玩家类
class Player implements Serializable {

    private static final long serialVersionUID = -3923850838982144178L;
    private String name; //昵称
    private int level; //经验值
    private int blood; //血值
    private Dog dog; //角色的宠物

    //private transient Dog dog; //角色的宠物,使用transient 表示该属性不会被序列化

    public Player() {
    }

    public Player(String name, int level, int blood, Dog dog) {
        this.name = name;
        this.level = level;
        this.blood = blood;
        this.dog = dog;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getLevel() {
        return level;
    }

    public void setLevel(int level) {
        this.level = level;
    }

    public int getBlood() {
        return blood;
    }

    public void setBlood(int blood) {
        this.blood = blood;
    }

    public Dog getDog() {
        return dog;
    }

    public void setDog(Dog dog) {
        this.dog = dog;
    }

    @Override
    public String toString() {
        return "Player{" +
                "name='" + name + '\'' +
                ", level=" + level +
                ", blood=" + blood +
                ", dog=" + dog +
                '}';
    }
}

public class SerializableDemo {
    public static void main(String[] args) {
        Dog dog = new Dog("二郎犬", 100);
        Player p = new Player("李逍遥", 1000, 300, dog);
        //对象序列化
        File saveFile = new File("io/save.dat");

        ObjectInput oin = null;
        ObjectOutput oout = null;
        try {
            //对象输出流
            oout = new ObjectOutputStream(new FileOutputStream(saveFile));
            //输入输入流
            oin = new ObjectInputStream(new FileInputStream(saveFile));

            oout.writeObject(p);
            oout.close();

            //读取文件，把对象恢复到内存里面,读盘。
            Player temp = (Player) oin.readObject();
            System.out.println(temp);
            System.out.println(temp == p);//false;说明反序列出来的是新对象。

        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            try {
                if (oin != null) {
                    oin.close();
                    oin = null;
                }
                if (oout != null) {
                    oout.close();
                    oout = null;
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }
}
